package com.itcast.dao;

import com.itcast.domain.Good;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodDao {
    /**
     * 查询所有
     * @return
     */
    @Select("select * from good")
    List<Good> findAll();

    /**
     * 创建商品
     * @param good
     * @return
     */
    @Insert("insert into good(name,price,weight,number,status,time,url) values(#{name},#{price},#{weight},#{number},#{status},#{time},#{url})")
    int saveGood(Good good);

    @Select("select * from good where name = #{name}")
    Good findByName(String name);

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Select("select * from good where id = #{id}")
    Good findById(String id);

    /**
     * 通过商品名称模糊查询
     * @param name
     * @return
     */
    @Select("select * from good where name like CONCAT('%',#{name},'%');")
    List<Good> findByNameLike(String name);

    /**
     * 更新商品
     * @param good
     * @return
     */
    @Update("<script>" +
            " update good " +
                "<set>" +
                    "<if test=\"name != null and name!=''\">" +
                        "name=#{name}," +
                    "</if>" +
                    "<if test=\"price != null and price!=''\">" +
                        "price=#{price}," +
                    "</if>" +
                    "<if test=\"weight != null and weight!=''\">" +
                        "weight=#{weight}," +
                    "</if>" +
                    "<if test=\"number != null and number!=''\">" +
                        "number=#{number}," +
                    "</if>" +
                    "<if test=\"status != null and status!=''\">" +
                        "status=#{status}," +
                    "</if>" +
                    "<if test=\"time != null and time!=''\">" +
                        "time=#{time}," +
                    "</if>" +
                    "<if test=\"url != null and url!=''\">" +
                        "url=#{url}" +
                    "</if>" +
                "</set>" +
            "where id=#{id}" +
            " </script>")
    int update(Good good);


    /**
     * 删除单个商品
     * @param id
     * @return
     */
    @Delete("delete from good where id = #{id}")
    int delete(String id);

    /**
     * 批量删除
     * @param list
     * @return
     */
    @Delete("<script>" +
            " delete from good " +
            "where id in" +
            " <foreach collection='list' item='id' open='(' separator=',' close=')'>" +
                "#{id}" +
            "</foreach>" +
            "</script>"
    )
    int batchDelete(List<String> list);

    /**
     * 批量上线
     * @param list
     * @return
     */
    @Update("<script>" +
                " update good " +
                " set status=1 " +
                "where id in" +
                " <foreach collection='list' item='id' open='(' separator=',' close=')'>" +
                    "#{id}" +
                "</foreach>" +
            "</script>"
    )
    int isOnline(List<String> list);

    /**
     * 批量下线
     * @param list
     * @return
     */
    @Update("<script>" +
            " update good " +
            " set status=0 " +
            "where id in" +
            " <foreach collection='list' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>"
    )
    int offLine(List<String> list);

}
